package com.winz.cultureapp.fragment;

import android.os.Bundle;
import androidx.recyclerview.widget.GridLayoutManager;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import com.mine.xrecyclerview.XRecyclerView;
import com.mine.xrecyclerview.rcvadapter.OnItemClickListener;
import com.winz.cultureapp.R;
import com.winz.cultureapp.activity.NewsDesActivity;
import com.winz.cultureapp.adapter.xrcv.MoreArtAdapter;
import com.winz.cultureapp.base.BaseFragment;
import com.winz.cultureapp.constants.Constants;
import com.winz.cultureapp.entity.ArtListBean;
import com.winz.cultureapp.http.OnResponeListener;
import com.winz.cultureapp.http.request.RequestFactory;
import com.winz.cultureapp.http.util.HttpUitls;
import com.winz.cultureapp.utils.JsonUtil;
import com.winz.cultureapp.utils.Logger;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 文物鉴赏详情
 */
public class MoreArtFragment extends BaseFragment implements OnItemClickListener, XRecyclerView.LoadingListener {
    @BindView(R.id.xrcv_art)
    public XRecyclerView mXrcv_art;

    private boolean isRefresh = true;
    private int limit = 12;
    private int page = 1;
    private int totalPage = 0;

    private List<ArtListBean.DataBean.ListBean> mDatas;
    private MoreArtAdapter mAdapter;
    private String cid;

    public static MoreArtFragment getInstance(String cid){

        MoreArtFragment fragment = new MoreArtFragment();
        Bundle bundle = new Bundle();
        bundle.putString(Constants.KEY.ART.ITEM_ID,cid);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int contentView() {
        return R.layout.fragment_more_art;
    }

    @Override
    protected void initViews(View view) {
        mDatas = new ArrayList<>();
        // XrecyclerView
        GridLayoutManager manager = new GridLayoutManager(mContext,3);
        mXrcv_art.setLayoutManager(manager);
        mXrcv_art.setPullRefreshEnabled(true);
        mXrcv_art.setLoadingMoreEnabled(true);

        mAdapter = new MoreArtAdapter(mContext,mDatas,R.layout.empty_view,R.layout.item_more_art);

    }

    @Override
    protected void initEvents() {

        mXrcv_art.setAdapter(mAdapter);
        mXrcv_art.setLoadingListener(this);
        mAdapter.setOnItemClickListener(this);

    }

    @Override
    protected void getDatas(Bundle arguments) {
        if (arguments != null) {
            cid = arguments.getString(Constants.KEY.ART.ITEM_ID);
            if (!TextUtils.isEmpty(cid)){
                getList();
            }else {
                showMsg("数据错误");
            }
        }
    }

    /**
     * 获取数据列表
     */


    private void getList() {
        showProgressDialog();

        HttpUitls.onGet("cultural", RequestFactory.getInstance().cultural(page+"",limit+"","0",cid), new OnResponeListener<ArtListBean>() {
            @Override
            public void onSuccess(ArtListBean bean) {
                Logger.d("cultural==>>", JsonUtil.getJsonFromObj(bean));
                if (bean.code == 0) {
                    totalPage = bean.totalPage;
                    if (bean.data.list != null && bean.data.list.size() > 0) {
                        mXrcv_art.setLoadingMoreEnabled(true);
                        if (isRefresh) {
                            mDatas.clear();
                            mDatas = bean.data.list;
                        } else {
                            mDatas.addAll(bean.data.list);
                        }
                    } else {
                        mDatas.clear();
                        ArtListBean.DataBean.ListBean empty = new ArtListBean.DataBean.ListBean();
                        empty.layout_type = -1;
                        mDatas.add(empty);
                        mXrcv_art.setLoadingMoreEnabled(false);
                    }
                    mAdapter.setDatas(mDatas);
                } else {
                    showMsg(bean.msg);
                }
                if (isRefresh) {
                    mXrcv_art.refreshComplete();
                } else {
                    mXrcv_art.loadMoreComplete();
                }
                dismissProgressDialog();
            }

            @Override
            public void onFailed(String error) {
                Logger.d("cultural==>>", error);
                dismissProgressDialog();
                showMsg(error);
                if (isRefresh) {
                    mXrcv_art.refreshComplete();
                } else {
                    mXrcv_art.loadMoreComplete();
                }
            }
        });
    }


    @Override
    public void onItemClick(ViewGroup parent, View view, int position) {
        Bundle bundle = new Bundle();
        bundle.putString(Constants.KEY.NEWS.ID,mDatas.get(position).museumGoodsId);
        bundle.putString(Constants.KEY.NEWS.TOP_TITLE,mDatas.get(position).title);
        bundle.putString(Constants.KEY.NEWS.HREF,Constants.URL.IP + "/cultural/detail/" + mDatas.get(position).museumGoodsId);
        startActivity(NewsDesActivity.class,false,bundle);

    }

    @Override
    public boolean onItemLongClick(ViewGroup parent, View view, int position) {
        return false;
    }

    /**
     * 刷新
     */
    @Override
    public void onRefresh() {
        isRefresh = true;
        page = 1;
        getList();
    }

    /**
     * 加载
     */
    @Override
    public void onLoadMore() {
        isRefresh = false;
        if (page>=totalPage){
            mXrcv_art.setNoMore(true,true,"没有更多数据了");
        }else {
            page++;
            getList();
        }
    }
}
